﻿Public Class frmTheLoai

    Private Sub btnLamLai_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnLamlai.Click
        PhucHoiNutSua()
        TxtMaTL.Text = ""
        txtTenTL.Text = ""

    End Sub

    Private Sub frmTheLoai_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        datTheLoai.DataSource = ClsTheLoai.DSTheLoai
    End Sub

    Private Sub btnXoa_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnXoa.Click
        PhucHoiNutSua()
        If MsgBox("Bạn có muốn xóa thể loại có mã thể loại =" & TxtMaTL.Text & " không ?", MsgBoxStyle.YesNo, "Thông báo") = MsgBoxResult.Yes Then
            If Not ClsTheLoai.Xoa(TxtMaTL.Text.Trim) Then
                MsgBox("Không thể xóa thể loại có mã " & TxtMaTL.Text & ".Có thể sách có thể loại này vẫn còn tồn tại!", MsgBoxStyle.Critical, "Loi")
            End If
            datTheLoai.DataSource = ClsTheLoai.DSTheLoai
        End If

    End Sub

    Private Sub datTheLoai_CellDoubleClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles datTheLoai.CellDoubleClick
        PhucHoiNutSua()
        btnSua_Click(sender, e)
    End Sub

    Private Sub btnSua_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSua.Click
        If btnSua.Text = "Sửa" Then

            txtTenTL.Text = datTheLoai.CurrentRow.Cells("TenTL").Value
            TxtMaTL.Text = datTheLoai.CurrentRow.Cells("MaTL").Value
            btnSua.Text = "Lưu lại"
        Else
            btnSua.Text = "Sửa"
            ClsTheLoai.Sua(TxtMaTL.Text, txtTenTL.Text)
            datTheLoai.DataSource = ClsTheLoai.DSTheLoai
            btnLamLai_Click(sender, e)
        End If

    End Sub

    Private Sub btnThoat_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnThoat.Click
        Me.Close()
    End Sub

   
    Private Sub btnThem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnThem.Click
        PhucHoiNutSua()
        ClsTheLoai.Them(TxtMaTL.Text, txtTenTL.Text)
        datTheLoai.DataSource = ClsTheLoai.DSTheLoai
        btnLamLai_Click(sender, e)
    End Sub
    Private Sub PhucHoiNutSua()
        If btnSua.Text = "Lưu lại" Then
            btnSua.Text = "Sửa"
        End If
    End Sub

    Private Sub btnTimKiem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnTimKiem.Click
        PhucHoiNutSua()
        datTheLoai.DataSource = ClsTheLoai.TimKiem(txtTuKhoa.Text)
    End Sub

    Private Sub datTheLoai_CellContentClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles datTheLoai.CellContentClick

    End Sub

    Private Sub btnReLoad_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnReLoad.Click
        datTheLoai.DataSource = ClsTheLoai.DSTheLoai
        txtTuKhoa.Text = ""


    End Sub
End Class